package com.itruth.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import net.sf.json.JSONObject;

public class JsonUtil {
	
	/**
	 * 将json格式数据流转换成json对象
	 * @param is
	 * @return
	 */
	public static JSONObject parseInputStream(InputStream is) throws IOException {

		JSONObject json = new JSONObject();

		BufferedReader reader = null;

		try {
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			int length = -1;

			char[] buffer = new char[1024];

			StringBuffer sb = new StringBuffer();

			while ((length = reader.read(buffer)) != -1) {
				sb.append(buffer, 0, length);
			}
			System.out.println(" ------ " + sb.toString());
			json = JSONObject.fromObject(sb.toString());

		} catch (IOException e) {

			throw e;

		} finally {

			try {
				if (null != reader) {
					reader.close();
					reader = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return json;

	}
	
}
